home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: how slow are virtual functions?
- Message-ID: <marnoldDnJ15M.IDF@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <4gioqc$9f0@onlink3.onlink.net>
- Date: Thu, 29 Feb 1996 07:36:57 GMT
- Sender: marnold@netcom23.netcom.com
-
- wfss16@onlink.net writes:
-
- >Can anyone give me some timings/good anology as to how much slower calling a virtual
- >function is as to a normal one?
-
- A virtual fucntion call is basically equivalent to a call through function
- pointer in C (or C++!). When you declare virtual functions, you cause the
- compiler to arrange for a table of function-pointers. Depending on which
- class an object is, it uses a slightly different version of the table. This
- table is commonly known as the "virtual table" or "vtbl" and most of the cost
- of arranging for it all is paid at compile-time. As I said, the only cost at
- run-time is basically the cost of an indirect function call.
-
- PFUNC pfunc = &SomeFunc;
-
- // when you compare "normal" functions to virtual functions, you
- // are basically comparing...
-
- SomeFunc(123); // ...this (a normal call)
-
- pfunc(123); // ...with this (an indirect call)
-
-
- If you think about it, if you wanted to implement virtual function-esque
- behavior yourself, what would you use? Tables of function pointers, of
- course. Depending on some condition, you'd change the function addresses
- contained in the table. This is what a C++ compiler does for you.
-
- Virtual functions aren't some magically expensive thing. They are just
- indirect function calls.
-
- If you want to exactly how your compiler implements virtual function
- calls, look at some assembly output sometime. It can be most revealing.
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-
-
-